/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import entity.MemberCustomer;
import entity.UserLogin;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.UserLoginServiceLocal;

/**
 *
 * @author SONY
 */
public class LoginStrutsAction extends org.apache.struts.action.Action {
    
    @EJB UserLoginServiceLocal userLoginServiceLocal;

    /* forward name="success" path="" */
    private static final String SUCCESS_ADMIN = "success_admin";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String email = "";
        String password = "";
        try {
            //store user object in the session-scope
            HttpSession session = request.getSession();
            if(session.getAttribute("user_admin") == null) {
                email = request.getParameter("email");
                password = request.getParameter("password");
                String remember = request.getParameter("remember");
                ServletContext context = servlet.getServletContext();
                userLoginServiceLocal = (UserLoginServiceLocal) context.getAttribute("userLoginServiceLocal");
                UserLogin user = userLoginServiceLocal.checkLogin(email, password);
                if (user != null) {
                    //Remember Account
                    int expiry = -1;
                    if ((remember != null) && (remember.equals("checked"))) {
                        expiry = 60 * 60 * 24 * 7; // 7 days
                    }
                    Cookie email_cookie = new Cookie("email_admin", email);
                    Cookie password_cookie = new Cookie("password_admin", password);
                    Cookie remember_cookie = new Cookie("remember_admin", remember);
                    email_cookie.setMaxAge(expiry);
                    password_cookie.setMaxAge(expiry);
                    remember_cookie.setMaxAge(expiry);
                    response.addCookie(email_cookie);
                    response.addCookie(password_cookie);
                    response.addCookie(remember_cookie);
                    
                    MemberCustomer member = user.getMemberID();
                    if(member == null) {
                        session.setAttribute("user_admin", user);
                        return mapping.findForward(SUCCESS_ADMIN);
                    }
                }
                if(session.getAttribute("user_admin") != null) {
                    return mapping.findForward(SUCCESS_ADMIN);
                }
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        request.setAttribute("errorMsg", "Email hoặc Mật Khẩu không đúng! Hãy kiểm tra lại.");
        request.setAttribute("email", email);
        request.setAttribute("password", password);       
        return mapping.findForward("error_admin");
    }
}
